<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        组织架构
        <el-divider />
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="gongsi1" /><span>
              武汉王河不溜东有限公司
            </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="chicken" />
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(scope.data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit? '编辑部门' : '添加部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hCloseDialog"
      >
        <deptDialog :id="curId" ref="deptDialog" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" />
      </el-dialog>
      <!--
        v-if:true。可见的。
        v-if: false 不可见。是直接移除（不创建)
      -->
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arrTree } from '@/utils/index'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      // 对话框显示
      showDialog: false,
      originList: [],
      // 依赖一份树形数据
      list: [],
      curId: '',
      isEdit: false
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 添加
    hAdd(id) {
      // 1. 获取当前被点击的部门的id
      console.log(id)
      this.curId = id
      // 2. 点击添加部门，显示弹框
      this.showDialog = true
      // 添加,编辑确认为false
      this.isEdit = false
    },
    // 编辑
    hEdit(id) {
      // 1. 获取当前被点击的部门的id
      console.log(id)
      this.curId = id
      // 2. 点击添加部门，显示弹框
      this.showDialog = true
      // 编辑,编辑确认为true
      this.isEdit = true
      // 获取子组件的引用
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.deptDialog)
        console.log('当前的curId', id)
        console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.id)
        // 调用子组件的方法
        this.$refs.deptDialog.loadDepartmentById()
      })
    },
    // 删除模块
    async doDel(id) {
      const res = await delDepartment(id)
      console.log(res)
      this.$message.success('删除成功')
      this.loadDepartments()
    },
    hDel(id) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
        this.loadDepartments()
      }).catch(() => {

      })
    },
    // 获取列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        res.data.depts.shift()
        // 组装一份简单的列表数据,给子组件用来做表单校验
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            name: item.name,
            code: item.code
          }
        })
        // 渲染数据
        this.list = arrTree(res.data.depts, '')
      } catch (err) {
        console.log(err)
      }
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    hClose() {
      this.showDialog = false
    },
    hCloseDialog() {
      this.$refs.deptDialog.resetForm()
    }
  }
}
</script>
